/*
 * @Author: tanjunyi@doctorwork.com
 * @Date: 2020-06-18 23:17:21
 * @LastEditors: tanjunyi@doctorwork.com
 * @LastEditTime: 2020-06-18 23:40:05
 * @Description:
 */

/**
 * 重写数组的 push unshift shift pop reverse sort splice
 * @param {*} data 
 */
let oldArraryMethods = Array.prototype;
//  value.__proto__ = oldArraryMethods;

export const arraryMethods = Object.create(oldArraryMethods);


const methods = ['push', 'shift', 'pop', 'sort', 'splice', 'reverse'];

methods.forEach(method => {
	arraryMethods[method] = function (...args) {

		//调用原生的数组方法  
		let inserted; //当前用户插入的元素
		switch (method) {
			case 'push':
			case 'unshift':
				inserted = args;
				break;
			case 'splice':
				inserted = args.slice(2);
			default:
				break;
		}
		let ob = this.__ob__;
		if (inserted) ob.observerArrary(inserted); //将新增属性继续观测
		// AOP 切片编程
		const result = oldArraryMethods[method].apply(this, args);
		return result;
	}
})
